import 'package:flutter/material.dart';
import '../pages/DemoPages.dart';
import '../pages/FormPage.dart';

import '../pages/TowPages.dart';
import '../pages/UserBean.dart';

void main() {
  runApp(MyApp());
}

//命名路由传值
//官方文档：https://docs.flutter.dev/cookbook/navigation/navigate-with-arguments
class MyApp extends StatelessWidget {
  MyApp({Key? key}) : super(key: key);

  Map routes = {
    "/": (context) => const MyHomePage(),
    "/demo": (context) => const DemoPage(),
    "/form": (context, {arguments}) => FormPage(arguments: arguments),
  };

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        initialRoute: '/',
        // routes: {}, 统一配置路由，所有相关的全部直接告诉当前界面路由信息，静态的
        //2、调用onGenerateRoute处理，动态的，我没有路由配置
        onGenerateRoute: (RouteSettings settings) {
          // 统一处理
          final String? name = settings.name;
          final Function? pageContentBuilder = routes[name];
          if (pageContentBuilder != null) {
            if (settings.arguments != null) {
              final Route route = MaterialPageRoute(
                  builder: (context) => pageContentBuilder(context,
                      arguments: settings.arguments));
              return route;
            } else {
              final Route route = MaterialPageRoute(
                  builder: (context) => pageContentBuilder(context));
              return route;
            }
          }
          return null;
        });
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("Flutter Appconst ")),
      body: TextButton(
        child: const Text("通过实例跳转"),
        onPressed: () async {
          // var result = await Navigator.pushNamed(context, "/demo");
          var result = await Navigator.pushNamed(context, '/form', arguments: {
            "title1": "data1",
            "title2": "data2",
            "title3": "data3",
            "title4": "data4",
          });
          print("result:$result");
        },
      ),
    );
  }
}
